﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace System
{
	public static class Dimensions
	{
		public const double NM = 1852.0;
		public const double Mile = 1609.344;
		public const double KM = 1000;
		public const double M = 1.0;
		public const double FT = 1 / 3.2808399;
		public const double MSL = FT * 100;
		public const double MpS = 1;
		public const double FpM = FT / 60;
		public const double KTS = NM / 3600;

		public static double Length(String Sou, String T)
		{
			Sou = Sou.ToLower().Replace(",", ".");
			String VS = Regex.Match(Sou, "[-+]?([0-9]*\\.[0-9]+|[0-9]+)").Captures[0].ToString();
			double v = double.Parse(VS, System.Globalization.CultureInfo.InvariantCulture);
			String D = Sou.Replace(VS, "");
			double[] Dim = new double[] { NM, KM, M, FT };
			return Length(v, D, T);
		}
		public static double Length(double Val, String Sou, String T)
		{
			List<String> Dims = new List<String>("nm,km,m,ft,MSL".Split(','));
			double[] Dim = new double[] { NM, KM, M, FT ,MSL};
			return Val * Dim[Dims.IndexOf(Sou)] / Dim[Dims.IndexOf(T)];
		}
		public static double Speed(String Sou, String T)
		{
			Sou = Sou.ToLower().Replace(",", ".");
			String VS = Regex.Match(Sou, "[-+]?([0-9]*\\,[0-9]+|[0-9]+)").Captures[0].ToString();
			double v = double.Parse(VS,System.Globalization.CultureInfo.InvariantCulture);
			String D = Sou.Replace(VS, "");
			return Speed(v, D, T);
		}
		public static double Speed(double Val, String Sou, String T)
		{
			List<String> Dims = new List<String>("kts,m/s,ft/min".Split(','));
			double[] Dim = new double[] { KTS, MpS, FpM };
			return Val * Dim[Dims.IndexOf(Sou)] / Dim[Dims.IndexOf(T)];
		}
		static double TAS_IAS(String Alt)
		{
			return 1 + 0.02 * Length(Alt, "ft") / 1000;
		}
		public static double TAS(double IAS, String Altitude)
		{
			return IAS * TAS_IAS(Altitude);
		}
		public static double IAS(double TAS, String Altitude)
		{
			return TAS / TAS_IAS(Altitude);
		}
	}
}